﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace Birthday
{
    public partial class StartForm : Form
    {
        private void lb_Person_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lb = (ListBox)sender;
            if (lb.SelectedIndex == -1)
                return;
            string[] parameters = this.GetSplitName(lb);

            Man man = DataLayer.GetManDetails(parameters[0], parameters[1]);

            SplitContainer sc =(SplitContainer)(((SplitterPanel)(lb.Parent)).GetContainerControl());

            Control.ControlCollection controlCollection= sc.Panel2.Controls;

            List<string> manInfo = new List<string>();


            foreach (string val in man.ManInfo.Values)
                manInfo.Add(val);

            int i = 0;
            foreach (Control c in controlCollection)
            {
                if (c.GetType() == typeof(MaskedTextBox))
                {
                    ((MaskedTextBox)c).Text = manInfo[i++];
                }

            }
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            Man newMan = new Man();

            newMan.Name = this.mtb_Name_createperson.Text.Trim();
            newMan.SurName = this.mtb_SurName_createperson.Text.Trim();
            newMan.DateOfBirth= this.mtb_Date_createperson.Text.Trim();
            newMan.Category= this.mtb_Relations_createperson.Text.Trim();
            newMan.Phone = this.mtb_Phone_createperson.Text.Trim();
            newMan.MobilePhone= this.mtb_MobPhone_createperson.Text.Trim();
            newMan.Country= this.mtb_Country_createperson.Text.Trim();
            newMan.City= this.mtb_City_createperson.Text.Trim();
            newMan.Street= this.mtb_Street_createperson.Text.Trim();
            newMan.House= this.mtb_House_createperson.Text.Trim();

            string message = string.Empty;
            DataLayer.AddPerson(newMan, ref message);
            MessageBox.Show(message);

            BindingData();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Подтвердите удаление","Delete Person",MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                /*TabPage tp = tc_Person.SelectedTab;
                Control.ControlCollection labelsAndMasked =
                   ((SplitContainer)((SplitContainer)tp.Controls["sc_DeletePerson"]).
                   Panel1.Controls["sc_DeletePerson_inner"]).Panel2.Controls;*/

                Man newMan = new Man();

                newMan.Name = this.mtb_Name_DeletePerson.Text.Trim();
                newMan.SurName = this.mtb_SurName_DeletePerson.Text.Trim();
                newMan.DateOfBirth = this.mtb_Date_DeletePerson.Text.Trim();
                newMan.Category = this.mtb_Relations_DeletePerson.Text.Trim();
                newMan.Phone = this.mtb_Phone_DeletePerson.Text.Trim();
                newMan.MobilePhone = this.mtb_MobPhone_DeletePerson.Text.Trim();
                newMan.Country = this.mtb_Country_DeletePerson.Text.Trim();
                newMan.City = this.mtb_City_DeletePerson.Text.Trim();
                newMan.Street = this.mtb_Street_DeletePerson.Text.Trim();
                newMan.House = this.mtb_House_DeletePerson.Text.Trim();

                if (DataLayer.DeletePerson(newMan))
                {
                    MessageBox.Show("Персона удалена из БД.");                    
                }
                BindingData();
                
            }
            return;
        }
    }
}
 